import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;

interface Response {
  result: Array<string>;
  result_code: string;
  length: number;
}

@Component({
  selector: 'app-privilege-table',
  templateUrl: './privilege-table.component.html'
})

export class PrivilegeTableComponent implements OnInit {

  @Input() tableNumber: number;

  @Output()
  beforeEditInfo: EventEmitter<TabVal> = new EventEmitter();

  public tabVal: string[];

  right: any = {
    "user": {},
    "privilege": {}
  };
  constructor(private server: Service, private http: HttpClient, private removeTableService: RemoveTableService, private tableListService: CudTableService, ) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    this.getTabList(1, 20, "0");
    //接受查询结果
    this.server.privilegeeventEmit.subscribe((value: any) => {
      this.tabVal = value;
      //console.log(this.tabVal)
    });
  }
  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();
  // 表格
  getTabList(pageNumber, pageSize, id) {

    let data: Pagination = new Pagination(pageNumber, pageSize, id);
    let url = this.server.baseUrl+'admin/operations/getPageList';
    //console.log(data)
    this.tableListService.postRequest(url, data).then((data: any) => {
      if (data.result_code == '0000') {
        this.tabVal = data.result;
        this.tabeListOutput.emit(data.length);
      }
      //console.log(data.result);
    }).catch();
  }

  @Output()
  detailsOutput: EventEmitter<TabVal> = new EventEmitter();

  // 详情（弹窗）
  detailsPopup(tabval) {
    $("#details").modal("show");
    this.outputTabVal(tabval);
    //console.log(tabval)
  }

  // 修改（弹窗）
  editPopup(tabval) {
    $("#edit").modal("show");
    this.outputTabVal(tabval);
    //console.log(tabval)
  }

  // 每一行的表格信息
  outputTabVal(tabval) {
    let tabVal: TabVal = new TabVal(tabval.operationId, tabval.operationCode, tabval.operationName, tabval.enable, tabval.description);
    this.detailsOutput.emit(tabVal);
  }

  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  public isSelect: boolean = true;
  public isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();

  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'operationId');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 单选（多选）
  chose(bool, operationId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, operationId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }

}


// 表格
export class TabVal {
  constructor(
    public operationId: string,
    public operationCode: string,
    public operationName: string,
    public enable: string,
    public description?: string
  ) { }
}

// 删除
// export class RmTab {
//   constructor(
//     public rmArr : Array<string> = []
//   ) { }
// }

